﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WoSoft.PACS.Data.TemplateData;
using WoSoft.PACS.Utility;
using DevComponents.DotNetBar;
using WoSoft.PACS.Data.CheckItem;

namespace WoSoft.PACS.Win.Forms.SubForms
{
    /// <summary>
    /// 提纲分类
    /// </summary>
    public partial class FrmCheckItemFl : FrmBase
    {
        private CheckItemDataService checkItemDataService = new CheckItemDataService();

        private string _categoryID;
        public FrmCheckItemFl(string categoryid)
        {
            InitializeComponent();
            _categoryID = categoryid;
        }

        //加载数据
        private void FrmOutlineCategory_Load(object sender, EventArgs e)
        {
            LoadCategoryData();

            if (!string.IsNullOrEmpty(this._categoryID))
            {
                //编辑模式下不可以连接添加
                this.ckKeepAdd.Checked = false;
                this.ckKeepAdd.Visible = false;                
            }
        }

        private void LoadCategoryData()
        {
            this.cbParent.DataSource = checkItemDataService.GetCheckItemFlAllData();
            this.cbParent.ValueMember = "ID";
            this.cbParent.DisplayMembers = "简称";
            this.cbParent.ParentFieldNames = "ID,上级ID";
            this.cbParent.SelectedIndex = -1;

            this.cbWords.DataSource = checkItemDataService.GetWordFl();
            this.cbWords.ValueMember = "ID";
            this.cbWords.DisplayMembers = "名称";
            this.cbWords.ParentFieldNames = "ID,名称";
            this.cbWords.SelectedIndex = -1;

            //加载数据
            if (string.IsNullOrEmpty(this._categoryID)) return;//新增分类
            var categoryDt = checkItemDataService.GetCheckItemFlByID(_categoryID);
            this.tbName.Text = categoryDt.GetColumnValue("名称").ToString();
            this.tbComment.Text = categoryDt.GetColumnValue("注释").ToString();
            this.cbParent.SelectedValue = categoryDt.GetColumnValue("上级ID");
            this.cbWords.SelectedValue = categoryDt.GetColumnValue("对应词语ID");
        }

        //确定保存
        private void btnOK_Click(object sender, EventArgs e)
        {
            this.errorProvider1.Clear();
            if (this.tbName.Text.Trim() == string.Empty)
            {
                this.errorProvider1.SetError(this.tbName, "请填写分类名称");
                return;
            }

            var cnt = 0;
            var cname = this.tbName.Text.Trim();
            var parentID = this.cbParent.SelectedValue != null ? this.cbParent.SelectedValue.ToString() : string.Empty;
            var wordID = this.cbWords.SelectedValue != null ? this.cbWords.SelectedValue.ToString() : string.Empty;
            var comment = this.tbComment.Text.Trim();
            //检查是否存在ID
            if (string.IsNullOrEmpty(this._categoryID))
            {
                cnt = checkItemDataService.CheckItemFlInsert(cname, parentID, wordID, comment);
            }
            else
            {//更新
                cnt = checkItemDataService.CheckItemFlUpdate(this._categoryID, cname, parentID, wordID, comment);
            }

            if (!this.ckKeepAdd.Checked)//非连续添加
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBoxEx.Show("增加成功");
                this.tbName.Text = "";
                this.tbComment.Text = "";
                return;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }
    }
}
